<?php

App::uses('CrudController', 'Controller');
App::uses('Line', 'Model');

class LinesController extends CrudController
{
    public $_model = 'Line';

    public function admin($areaId, $moeId) {
        $this->request->allowMethod('get');
        $this->loadModel('Area');
        $this->loadModel('Moe');
        $appBosch = new stdClass();
        $appBosch->moeId = $moeId;
        $appBosch->areaId = $areaId;
        $appBosch->types = Line::getTypes();
        $description = $this->getDescription($areaId, $moeId);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Líneas de producción'));
        $this->set('description', $description);
    }

    private function getDescription($areaId, $moeId) {
        $this->Moe->id = $moeId;
        $moe = $this->Moe->read();
        $this->Area->id = $areaId;
        $area = $this->Area->read();
        $parts = array(
            $moe['Moe']['name'],
            $area['Area']['name'],
        );
        return implode(' - ', $parts);
    }

    public function getLinesByArea($all = null)
    {
        $this->request->allowMethod('get');
        $areaId = $this->request->query['filter']['filters'][0]['value'];
        $m = $this->_model;
        $records = $this->$m->getEnabledByAreaBasic($areaId);
        if ($all != null){
            array_unshift($records, array("text" => 'Todas', "value" => '0'));
        }
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function getLinesForCombo()
    {
        $this->request->allowMethod('get');
        $m = $this->_model;
        $records = $this->$m->getEnabledWithHierarchy();
        array_unshift($records, array("text" => 'Todas', "value" => '0'));
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($param) {
        $m = $this->_model;
        $records = $this->$m->getEnabledByArea($param);
        return $records;
    }

    protected function c($model) {
        return array(
            'area_id' => $model->area_id,
            'name' => mb_strtoupper($model->text),
            'text' => mb_strtoupper($model->text),
            'type' => $model->type,
            'colour' => $model->colour,
            'number' => rand(0, 100000),
        );
    }

    protected function u($model) {
        return array(
            'area_id' => $model->area_id,
            'name' => mb_strtoupper($model->text),
            'text' => mb_strtoupper($model->text),
            'type' => $model->type,
            'colour' => $model->colour,
        );
    }

    protected function id($model) {
        return $model->value;
    }

    protected function beforeCreate(&$newRecord)
    {
        $m = $this->_model;
        $res = $this->$m->findByNumber($newRecord['number']);
        while(isset($res[$m]['id'])) {
            $newRecord['number'] = rand(0, 100000);
        }
        return true;
    }
}
